package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.system.entity.Modify;
import cn.stylefeng.guns.modular.system.model.ModifyDto;
import cn.stylefeng.guns.modular.system.service.ModifyService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * 维修记录控制器
 *
 * @author LST
 * @Date 2019
 */
@Controller
@RequestMapping("/modify")
public class ModifyController extends BaseController {

    private static String PREFIX = "/system/car/";

    @Autowired
    private ModifyService modifyService;
    /**
     * 跳转到添加维修记录
     * @return
     */
    @RequestMapping("/modify_add")
    public String addView() {
        return PREFIX + "modify_add.html";
    }
    /**
     * 跳转到查看维修记录
     */
    @Permission
    @RequestMapping("/car_modify")
    public String userEdit(@RequestParam Long modifyId) {
        if (ToolUtil.isEmpty(modifyId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //获取信息
        Modify modify = this.modifyService.getById(modifyId);
        //
        LogObjectHolder.me().set(modify);
        return PREFIX + "car_modify.html";
    }
    /**
     * 添加维修记录
     *
     * @author fengshuonan
     */
    @RequestMapping("/add")
    @Permission
    @ResponseBody
    public Long add(@Valid ModifyDto modifyDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Modify modify = new Modify();
        modify.setModifyNote(modifyDto.getModifyNote());
        modify.setModifyDate(modifyDto.getModifyDate());
        modifyService.insertAndGetId(modify);
        return modify.getModifyId();
    }


    /**
     * 获取维修记录详情
     *
     * @author fengshuonan
     */
    @RequestMapping("/getModifyInfo")
    @ResponseBody
    public Object getModifyInfo(@RequestParam Long modifyId) {
        if (ToolUtil.isEmpty(modifyId)) {
            throw new RequestEmptyException();
        }
        Modify modify = modifyService.getById(modifyId);
        return ResponseData.success(modify);
    }
}